package com.frinder.robot.strategy.task.distribute.biz;

import com.frinder.robot.handler.task.RobotTask;
import com.frinder.robot.strategy.task.distribute.DistributeTaskToRobotBaseStrategy;
import com.frinder.robot.strategy.task.distribute.DistributeTaskToRobotPayload;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.Objects;

import static com.frinder.robot.strategy.task.distribute.DistributeTaskToRobotPayload.Type.PRIORITY;

/**
 * @ClassName DistributeTaskToRobotPriorityStrategy
 * @Description 根据 优先级（值越大越优先） 分派任务
 * @Author frinder
 * @Date 2019/10/19 14:35
 * @Verion 1.0
 */
@Slf4j
@Component
public class DistributeTaskToRobotPriorityStrategy extends DistributeTaskToRobotBaseStrategy {

    @Override
    public void handle(DistributeTaskToRobotPayload payload) {
        if (Objects.equals(payload.getType(), PRIORITY)) {
            distribute(payload, RobotTasks.values()
                    .stream()
                    .filter(o -> !o.isSend())
                    .sorted(Comparator.comparingInt(RobotTask::getPriority))
                    .findFirst()
            );
        } else {
            super.handle(payload);
        }
    }
}
